package com.item3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;



import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;


import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.util.List;

import static androidx.viewpager2.widget.ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT;


public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private ViewPager2 viewPager;
    private static final String[] contents = {"首页","论坛","更多"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        //获取按钮控件
        Log.e("ICON_Listener", "" + bottomNavigationView.getMenu().findItem(R.id.home));

        //导航栏重选事件：BottomNavigationView.OnNavigationItemReselectedListener只通知一次
        //导航栏选择事件
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Log.e("OnClick", "" + item.toString());
                switch (item.getItemId()) {
                    case R.id.home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.message:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.many:
                        viewPager.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });

    }


    private void init() {
        //数据初始化
        bottomNavigationView = findViewById(R.id.nav_view);
        viewPager = findViewById(R.id.viewpager);
        //取消默认的选中状态
        bottomNavigationView.setItemIconTintList(null);
        bottomNavigationView.setItemTextColor(null);


        //禁用预加载
        viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT_DEFAULT);
        viewPager.setAdapter(new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                return BNV_Fragment.newInstance(contents[position]);        //绑定ViewPage--Fragment
            }

            @Override
            public int getItemCount() {
                return contents.length;
            }
        });
        //页面改变监听
        viewPager.registerOnPageChangeCallback(changeCallback);

    }
    //页面改变监听
    private ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            //Log.e("滑动监听", "onPageSelected: "+"getMenu"+bottomNavigationView.getMenu() );
            bottomNavigationView.getMenu().getItem(position).setChecked(true);


        }
    };
    @Override
    protected void onDestroy() {
        //销毁前调用
        viewPager.unregisterOnPageChangeCallback(changeCallback);
        super.onDestroy();
    }


}

